package com.dd.ent.car.ui.home.templet;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.User;
import com.dd.ent.car.common.bean.note.Note;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.http.ImageLoaderUtil;
import com.dd.ent.car.common.util.DipPixelUtil;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.service.video.VideoService;
import com.dd.ent.car.ui.user.UserInfo;
import com.dd.ent.car.ui.widget.CircleImageView;
import com.pp.ent.Cherry;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import static com.dd.ent.car.common.config.UIConfig.NOTIFY_NOTE_UPDATE_COMMENT_COUNT;
import static com.dd.ent.car.common.config.UIConfig.NOTIFY_VIDEO_UPDATE_COMMENT_COUNT;

/**
 * Created by dengqu on 2016/9/14.
 */
public class HomeBottomView extends FrameLayout implements View.OnClickListener {
    private CircleImageView circleImageView;
    private TextView texUsername;
    private TextView mPopupView;
    private TextView texLike;
    private TextView texComment;
    private TextView textPlay;
    private PopupWindow mPopupWindow;
    private AlphaAnimation mAa;
    private Video mVideo;
    private int mPosition;
    @Inject
    private VideoService videoService;
    @Inject
    private UserService userService;

    private BottomItemViewOnClickListener bottomItemViewOnClickListener;

    public HomeBottomView(Context context) {
        super(context);
        init(context);
    }

    public HomeBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        initResource(context);
        initPopupWindow(context);
        initListeners();

    }


    private void initResource(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_list_bottom, this, true);
        circleImageView = (CircleImageView) root.findViewById(R.id.img_thumb);
        texUsername = (TextView) root.findViewById(R.id.tex_username);
        texLike = (TextView) root.findViewById(R.id.tex_like);
        texComment = (TextView) root.findViewById(R.id.tex_comment);
        textPlay = (TextView) root.findViewById(R.id.tex_play);
    }

    private void initPopupWindow(Context context) {
        mPopupView = new TextView(context);
        mPopupView.setText("+1");
        mPopupView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        mPopupView.setTextColor(0xff1294f6);

        mPopupWindow = new PopupWindow(mPopupView, -2, -2);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        mAa = new AlphaAnimation(1.0f, 0.0f);
        mAa.setDuration(1300);
    }

    private void initListeners() {

        texLike.setOnClickListener(this);
        texComment.setOnClickListener(this);
        mAa.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mPopupWindow.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void refreshData(int position, Video video) {
        mPosition = position;
        mVideo = video;
        if (video != null) {
            texLike.setText(String.valueOf(video.getLikeCount()));
            texComment.setText(String.valueOf(video.getCommentCount()));
            textPlay.setText(String.valueOf(video.getPlayCount()));
            User user = video.getUser();
            if (user != null) {
                ImageLoaderUtil.displayImage(user.getAvathor(), circleImageView);
                texUsername.setText(user.getUserName());
            }
        }
    }


    @UIHandler(NOTIFY_VIDEO_UPDATE_COMMENT_COUNT)
    public void onVideoUpdateCommentCount(Video video) {
        if (video != null && mVideo != null) {
            if (TextUtils.equals(video.getUuid(), mVideo.getUuid())) {
                refreshData(mPosition, video);
            }
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Cherry.injectObject(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Cherry.remove(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tex_like:
                setLikeClick(v, getContext());
                if (bottomItemViewOnClickListener != null) {
                    bottomItemViewOnClickListener.onLikeClick(v);
                }
                break;
            case R.id.tex_comment:
                if (bottomItemViewOnClickListener != null) {
                    bottomItemViewOnClickListener.onCommentClick(v);
                }
                break;
            case R.id.img_share:
                if (bottomItemViewOnClickListener != null) {
                    bottomItemViewOnClickListener.onShareClick(v);
                }
                break;

        }
    }

    private void setLikeClick(View view, Context context) {
        if (mVideo != null) {
            mVideo.setLikeCount(mVideo.getLikeCount() + 1);
            refreshData(mPosition, mVideo);
            videoService.addLikeToVideo(mVideo.getUuid(), 1, userService.getUid() == null ? "" : userService.getUid());
        }
        texLike.setSelected(true);
        mPopupWindow.showAsDropDown(view, DipPixelUtil.dip2px(context, 37), -DipPixelUtil.dip2px(context, 46));
        mPopupView.startAnimation(mAa);

        Animation scaleIn = AnimationUtils.loadAnimation(context, R.anim.scale_in);
        final Animation scaleOut = AnimationUtils.loadAnimation(context, R.anim.scale_out);
        scaleIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                texLike.startAnimation(scaleOut);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        texLike.startAnimation(scaleIn);

    }

    public void setBottomItemViewOnClickListener(BottomItemViewOnClickListener bottomItemViewOnClickListener) {
        this.bottomItemViewOnClickListener = bottomItemViewOnClickListener;
    }

    public interface BottomItemViewOnClickListener {
        void onLikeClick(View view);

        void onCommentClick(View view);

        void onShareClick(View view);
    }

}

